* Creates Figure A7
clear all

use "$savedata/masterdata.dta", replace

keep if sample25==1
gen vol = vol25

* Merge in trust characteristics
gen trust = trust_code
merge m:1 trust using "$inputs/types.dta"
drop if _merge==2
rename _merge _mergetrust
merge m:1 trust using "$staffdata/cardiologists_201718.dta"
drop if _merge==2
rename _merge _mergetrust2

* Create quality metric

*preserve
reghdfe survive30 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe30, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe30 = docfe30*signal_2step

drop residual signal* sigma*

reghdfe survive365 c.prevyear_cost, absorb(i.sex##i.derv_age i.black i.mixed i.chinese i.asian i.race_miss i.ynch* i.prevyear_stroke i.di1 i.di2 i.di3 i.di4 i.di5 i.shock i.arythmia i.arthero i.arrest i.dow##i.admidate_mont##i.finyear, savefe) keepsingleton
predict residual, residuals

xtreg residual c.std_ami3 c.std_nonami3, fe i(doctor_id)
predict docfe365, u
gen sigma_u = e(sigma_u)
gen sigma_e = e(sigma_e)

gen signal_2step = ((sigma_u^2) / ((sigma_u^2) + ((sigma_e^2)/vol)))
gen adj_docfe365 = docfe365*signal_2step

gen teach = 0
replace teach = 1 if trusttype==6
drop teaching

keep if finyear==2017 

gen min_docfe30 = adj_docfe30
gen max_docfe30 = adj_docfe30

gen min_docfe365 = adj_docfe365
gen max_docfe365 = adj_docfe365

keep if has_cea!=.

collapse (mean) adj_docfe* has_cea count fte_contracted fte_actual british british_trained age female teach trusttype tenure_orig part_time_contracted (min) min_docfe* (max) max_docfe* (p10) p10_docfe30 = adj_docfe30 p10_docfe365 = adj_docfe365 (p90) p90_docfe30 = adj_docfe30 p90_docfe365 = adj_docfe365 (sum) one, by(trust_code)


xtile rank30 = adj_docfe30, n(113)
xtile rank365 = adj_docfe365, n(113)

foreach var in adj_docfe30 adj_docfe365 min_docfe30 min_docfe365 max_docfe30 max_docfe365 p10_docfe30 p10_docfe365 p90_docfe30 p90_docfe365 {
	replace `var' = `var'*100
}

* DO 90-10

twoway scatter adj_docfe30 rank30, msymbol(dot) mcolor(black) msize(tiny) || rcap p10_docfe30 p90_docfe30 rank30, lcolor(gs8) lpattern(dash) xtitle(""Rank (Average 30-day survival)"") ytitle("Average doctor FE (30-day survival)") graphregion(color(white)) yscale(range(-6(1)6)) ylabel(-6(1)6) legend(off)
graph export "$results/figA7a.pdf", as(pdf) replace

twoway scatter adj_docfe365 rank365, msymbol(dot) mcolor(black) msize(tiny) || rcap p10_docfe365 p90_docfe365 rank365, lcolor(gs8) lpattern(dash) xtitle("Rank (Average 1-year survival)") ytitle("Average doctor FE (1-year survival)") legend(order(1 "Average FE" 2 "Within-hospital range") ) graphregion(color(white)) yscale(range(-6(1)6)) ylabel(-6(1)6)
graph export "$results/figA7b.pdf", as(pdf) replace
